package czy.demo.view;

import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import czy.demo.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class PDFView extends AbstractPdfView {

    private static final Logger logger = LoggerFactory.getLogger(PDFView.class);

    /**
     * 继承抽象的PDF视图只需要实现这一个方法，即构建Document，文档如何写出不需要操作
     * @param model：spring-mvc的模型数据
     * @param document：PDF文档
     * @param writer：写出
     * @param request：请求
     * @param response：响应
     */
    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<User> users = (List<User>) model.get("users");
        /* 默认情况下中文写出为空，入药这个东西才能写出中文 */
        BaseFont chinese = BaseFont.createFont("C://Windows//Fonts//simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font font = new Font(chinese, 12,Font.NORMAL);
        for (User user:users) {
            String content = "姓名："+user.getName()+" 年龄："+user.getAge();
            document.add(new Paragraph(content, font));
        }
    }
}
